package com.assets.Repairs;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.assets.Adapter.Addrepairs_Adapter1;
import com.assets.bean.AddRepairs_bean1;
import com.assets.newNHF.util.LogUtils;
import com.assets.util.TrustAllCerts;
import com.demo.WaveSwipeRefreshLayout;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.bean.Property_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/18.
 */

public class Repairs_property extends Activity {
    private ListView lv_poperty;
    private TextView title_text;
    private ImageView fh;
    private List<AddRepairs_bean1> data = new ArrayList<>();
    private WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;
    private String repairsId;
    private String url = "http://" + Url.IP + "/api/repairsQueryById";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repairs_property);
        inview();
        load();
    }

    private void inview() {
        fh = (ImageView) findViewById(R.id.fh);
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_text = (TextView) findViewById(R.id.title_text);
        title_text.setText("报修列表");
        Intent intent = getIntent();
        final String state = intent.getStringExtra("state");
        repairsId = intent.getStringExtra("repairsId");
        lv_poperty = (ListView) findViewById(R.id.repairs_list);
        lv_poperty.setOnItemClickListener((parent, view, position, id) -> {
            if (state.equals("预约报修") || state.equals("维修中")) {
                if (TextUtils.isEmpty(data.get(position).getStatus())) return;
                if (data.get(position).getStatus().equals("0")) {
                    Intent intent1 = new Intent(Repairs_property.this, Repairs_data.class);
                    intent1.putExtra("name", data.get(position).getAsset_name());
                    intent1.putExtra("number", data.get(position).getCode_number());
                    intent1.putExtra("describe", data.get(position).getRemark());
                    startActivity(intent1);
                } else {
                    Toast.makeText(getApplication(), "待修复才可上传附件", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) findViewById(R.id.main_swipe);
        mWaveSwipeRefreshLayout.setColorSchemeColors(Color.WHITE, Color.WHITE);//设置水滴中旋转动画的颜色，参数1:水滴落下过程中的颜色，参数2:水滴已经落下后的颜色
        mWaveSwipeRefreshLayout.setWaveColor(0xFF3794fc);//设置水滴样色
        mWaveSwipeRefreshLayout.setMaxDropHeight(1500);//设置最大下拉高度
        mWaveSwipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {//模拟3秒的加载过程
                    @Override
                    public void run() {
                        load();
                        //接下来可以刷新ListView
                    }
                }, 1200);


            }

        });
    }

    private void load() {
        new Thread() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient
                            .Builder()
                            .hostnameVerifier(new AllowAllHostnameVerifier())
                            .sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), TrustAllCerts.getX509TrustManager())
                            .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                            .readTimeout(500, TimeUnit.SECONDS)
                            .build();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("repairsId", repairsId)
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    LogUtils.d(responseData);
                    showResponse(responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void showResponse(final String responseData) {
        runOnUiThread(() -> {
            if (responseData.equals("[]")) {
                Toast.makeText(Repairs_property.this, "无数据", Toast.LENGTH_SHORT).show();
                finish();
            }
            data.clear();
            mWaveSwipeRefreshLayout.setRefreshing(false);//停止加载动画
            Gson gson = new Gson();
            List<Property_bean> userList = gson.fromJson(responseData, new TypeToken<List<Property_bean>>() {
            }.getType());
            for (int i = 0; i < userList.size(); i++) {
                String assetName = userList.get(i).getAssetName();
                String deptId = userList.get(i).getDeptId();
                String assetNumber = userList.get(i).getAssetNumber();
                String userId = userList.get(i).getUserId();
                String model = userList.get(i).getModel();
                String type = userList.get(i).getType();
                String remark = userList.get(i).getDetail();
                String image = userList.get(i).getImage();
                if (!TextUtils.isEmpty(image)) {
                    image = "http://" + Url.IP + image;
                }
                String status = userList.get(i).getStatus();
                data.add(new AddRepairs_bean1(image, assetName, deptId, assetNumber, userId, model, type, remark, status));
            }
            Addrepairs_Adapter1 adapter_item = new Addrepairs_Adapter1(Repairs_property.this, R.layout.input_adapter_item, data, "1");
            lv_poperty.setAdapter(adapter_item);
        });
    }

    @Override
    protected void onRestart() {
        load();
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.d("finish");
    }
}
